//
//  LookFriendsCell.m
//  PigMarket
//
//  Created by mac on 2019/7/2.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "LookFriendsCell.h"

@interface LookFriendsCell ()
/**
 头像
 */
@property (nonatomic , strong) UIImageView *headImg;
/**
 sex
 */
@property (nonatomic , strong) UIImageView *sexIcon;
/**
 name
 */
@property (nonatomic , strong) UILabel *nameLab;
/**
 加好友
 */
@property (nonatomic , strong) UIButton *addFriendOrChatBtn;
/**
 line
 */
@property (nonatomic , strong) UIView *line;
@end
@implementation LookFriendsCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    LookFriendsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[LookFriendsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.headImg];
    [self.contentView addSubview:self.sexIcon];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.addFriendOrChatBtn];
    [self.contentView addSubview:self.line];

}

- (void)initLayout {
    // 布局子控件
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
        make.size.mas_equalTo(50);
    }];
    [self.sexIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.headImg.mas_right).offset(-2);
        make.bottom.mas_equalTo(self.headImg.mas_bottom);
        make.size.mas_equalTo(14);
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(22);
        make.left.mas_equalTo(self.headImg.mas_right).offset(20);
    }];
    [self.addFriendOrChatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(56, 27));
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab.mas_left);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}
- (void)addFriendOrChatBtnClick {
    if (self.addFriendOrChatBlock) {
        self.addFriendOrChatBlock();
    }
}
#pragma mark - getter and setter
- (void)setModel:(SamePenFriend *)model {
    _model = model;
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    self.nameLab.text = model.nickName;
    if ([model.friendIf intValue] == 0) {
        [_addFriendOrChatBtn setTitle:@"加好友" forState:UIControlStateNormal];
    } else if ([model.friendIf intValue] == 1) {
        [_addFriendOrChatBtn setTitle:@"聊一聊" forState:UIControlStateNormal];
    }
    switch ([model.sex integerValue]) {
        case SexTypeMan:
        {
            self.sexIcon.image = [UIImage imageNamed:@"user_man"];
        }
            break;
        case SexTypeWoman:
        {
            self.sexIcon.image = [UIImage imageNamed:@"user_woman"];
        }
            break;
        default:
            break;
    }
}
- (UIImageView *)headImg {
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        _headImg.layer.cornerRadius = 25;
        _headImg.layer.masksToBounds = YES;
    }
    return _headImg;
}
- (UIImageView *)sexIcon {
    if (!_sexIcon) {
        _sexIcon = [[UIImageView alloc] init];
    }
    return _sexIcon;
}
- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = [UIColor textBlackColor];
        _nameLab.font = [UIFont systemFontOfSize:16];
    }
    return _nameLab;
}
- (UIButton *)addFriendOrChatBtn {
    if (!_addFriendOrChatBtn) {
        _addFriendOrChatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addFriendOrChatBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        _addFriendOrChatBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _addFriendOrChatBtn.layer.cornerRadius = 5;
        _addFriendOrChatBtn.layer.borderWidth = 1;
        _addFriendOrChatBtn.layer.borderColor = [UIColor primaryColor].CGColor;
        [_addFriendOrChatBtn addTarget:self action:@selector(addFriendOrChatBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addFriendOrChatBtn;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    }
    return _line;
}
@end
